package com.byb.server.service.impl;

import com.byb.server.service.CountryStrichenService;
import com.byb.server.domain.CountryStrichen;
import com.byb.server.repository.CountryStrichenRepository;
import com.byb.server.repository.search.CountryStrichenSearchRepository;
import com.byb.server.service.dto.CountryStrichenDTO;
import com.byb.server.service.mapper.CountryStrichenMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CountryStrichen.
 */
@Service
@Transactional
public class CountryStrichenServiceImpl implements CountryStrichenService{

    private final Logger log = LoggerFactory.getLogger(CountryStrichenServiceImpl.class);

    private final CountryStrichenRepository countryStrichenRepository;

    private final CountryStrichenMapper countryStrichenMapper;

    private final CountryStrichenSearchRepository countryStrichenSearchRepository;
    public CountryStrichenServiceImpl(CountryStrichenRepository countryStrichenRepository, CountryStrichenMapper countryStrichenMapper, CountryStrichenSearchRepository countryStrichenSearchRepository) {
        this.countryStrichenRepository = countryStrichenRepository;
        this.countryStrichenMapper = countryStrichenMapper;
        this.countryStrichenSearchRepository = countryStrichenSearchRepository;
    }

    /**
     * Save a countryStrichen.
     *
     * @param countryStrichenDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public CountryStrichenDTO save(CountryStrichenDTO countryStrichenDTO) {
        log.debug("Request to save CountryStrichen : {}", countryStrichenDTO);
        CountryStrichen countryStrichen = countryStrichenMapper.toEntity(countryStrichenDTO);
        countryStrichen = countryStrichenRepository.save(countryStrichen);
        CountryStrichenDTO result = countryStrichenMapper.toDto(countryStrichen);
       // countryStrichenSearchRepository.save(countryStrichen);
        return result;
    }

    /**
     *  Get all the countryStrichens.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<CountryStrichenDTO> findAll(Pageable pageable) {
        log.debug("Request to get all CountryStrichens");
        return countryStrichenRepository.findAll(pageable)
            .map(countryStrichenMapper::toDto);
    }

    /**
     *  Get one countryStrichen by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public CountryStrichenDTO findOne(Long id) {
        log.debug("Request to get CountryStrichen : {}", id);
        CountryStrichen countryStrichen = countryStrichenRepository.findOne(id);
        return countryStrichenMapper.toDto(countryStrichen);
    }

    /**
     *  Delete the  countryStrichen by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete CountryStrichen : {}", id);
        countryStrichenRepository.delete(id);
        //countryStrichenSearchRepository.delete(id);
    }

    /**
     * Search for the countryStrichen corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<CountryStrichenDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of CountryStrichens for query {}", query);
        Page<CountryStrichen> result = countryStrichenSearchRepository.search(queryStringQuery(query), pageable);
        return result.map(countryStrichenMapper::toDto);
    }
}
